//
//  DianYingCollectionViewCell.m
//  C17
//
//  Created by dllo on 16/3/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "DianYingCollectionViewCell.h"
#import "DianyingImageCollectionViewCell.h"
#import "DianYingTableViewCell.h"
#import "MPViewController.h"

@interface DianYingCollectionViewCell ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,assign)CGSize gao;
@property (nonatomic,strong) MPMoviePlayerViewController *moviePlayerViewController;

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowlayout;

@end

@implementation DianYingCollectionViewCell

- (void)awakeFromNib {
    // Initialization code
    
    [self.DianYingCollection registerNib:[UINib nibWithNibName:NSStringFromClass([DianyingImageCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"DianYingCollection"];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([DianYingTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"movieTableView"];
    self.PlayMovie.layer.borderWidth=2;
    self.PlayMovie.layer.masksToBounds=YES;
    self.PlayMovie.layer.cornerRadius=20;
    self.PlayMovie.layer.borderColor=[[UIColor whiteColor]CGColor];
    [self.PlayMovie addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchDown];
    
    //self.flowlayout.itemSize=CGSizeMake(WIDTH, HEIGHT+40);
}

-(void)setIndexpathCon:(NSIndexPath *)indexpathCon
{
    [self.DianYingCollection scrollToItemAtIndexPath:indexpathCon atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DianYingTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"movieTableView" forIndexPath:indexPath];
    cell.movielabel.text=self.text;
    self.gao=[self sizeToFit:cell.movielabel size:CGSizeMake(self.frame.size.width-37, MAXFLOAT)];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.gao.height+20;
}
//计算label长度
-(CGSize)sizeToFit:(UILabel *)label size:(CGSize)size{
    CGSize realSize = [label.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:label.font} context:nil].size;
    return realSize;
}

-(void)click{
    if (self.webViewUrl!=nil) {
        MPViewController *vc=[[MPViewController alloc] init];
        vc.webViewUrl=self.webViewUrl;
        vc.webViewName=self.webViewName;
        self.webViewUrl=nil;
        [self.root.navigationController pushViewController:vc animated:YES];
    }
    else{
        self.moviePlayerViewController=nil;//保证每次点击都重新创建视频播放控制器视图，避免再次点击时由于不播放的问题
        //    [self presentViewController:self.moviePlayerViewController animated:YES completion:nil];
        //注意，在MPMoviePlayerViewController.h中对UIViewController扩展两个用于模态展示和关闭MPMoviePlayerViewController的方法，增加了一种下拉展示动画效果
        [self.root presentMoviePlayerViewControllerAnimated:self.moviePlayerViewController];
        //强制切换横竖屏
//        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
    }
}






//-(void)setYOffect:(CGFloat)yOffect{
//    if (_yOffect!=yOffect) {
//        _yOffect = yOffect;
//    }
//    self.DianYingCollection.contentOffset = CGPointMake(-yOffect+yOffect*3,0);
//    NSLog(@"%f",self.DianYingCollection.contentOffset.x);
//    [self.DianYingCollection reloadData];
//}



#pragma mark - 私有方法
/**
 *  取得本地文件路径
 *
 *  @return 文件路径
 */
-(NSURL *)getFileUrl{
    NSString *urlStr=[[NSBundle mainBundle] pathForResource:@"The New Look of OS X Yosemite.mp4" ofType:nil];
    NSURL *url=[NSURL fileURLWithPath:urlStr];
    return url;
}

/**
 *  取得网络文件路径
 *
 *  @return 文件路径
 */
-(NSURL *)getNetworkUrl{
    NSString *urlStr=self.videoUrl;
    urlStr=[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url=[NSURL URLWithString:urlStr];
    return url;
}

-(MPMoviePlayerViewController *)moviePlayerViewController{
    if (!_moviePlayerViewController) {
        NSURL *url=[self getNetworkUrl];
        _moviePlayerViewController=[[MPMoviePlayerViewController alloc]initWithContentURL:url];
        [self addNotification];
    }
    return _moviePlayerViewController;
}



#pragma mark - 控制器通知
/**
 *  添加通知监控媒体播放控制器状态
 */
-(void)addNotification{
    NSNotificationCenter *notificationCenter=[NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackStateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayerViewController.moviePlayer];
    [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayerViewController.moviePlayer];
    
}

/**
 *  播放状态改变，注意播放完成时的状态是暂停
 *
 *  @param notification 通知对象
 */
-(void)mediaPlayerPlaybackStateChange:(NSNotification *)notification{
    switch (self.moviePlayerViewController.moviePlayer.playbackState) {
        case MPMoviePlaybackStatePlaying:
            NSLog(@"正在播放...");
            break;
        case MPMoviePlaybackStatePaused:
            NSLog(@"暂停播放.");
            break;
        case MPMoviePlaybackStateStopped:
            NSLog(@"停止播放.");
            break;
        default:
            NSLog(@"播放状态:%li",self.moviePlayerViewController.moviePlayer.playbackState);
            break;
    }
}

/**
 *  播放完成
 *
 *  @param notification 通知对象
 */
-(void)mediaPlayerPlaybackFinished:(NSNotification *)notification{
    NSLog(@"播放完成.%li",self.moviePlayerViewController.moviePlayer.playbackState);
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
}

-(void)dealloc{
    //移除所有通知监控
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
